// 虚拟数据服务
import { ref } from 'vue'

// 生成随机ID
export const generateId = () => Math.random().toString(36).substring(2, 10)

// 生成随机日期时间
export const randomDate = (start = new Date(2023, 0, 1), end = new Date()) => {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
}

// 格式化日期时间
export const formatDate = (date) => {
  return date.toLocaleString('zh-CN')
}

// 随机选择数组中的一项
export const randomItem = (array) => {
  return array[Math.floor(Math.random() * array.length)]
}

// 随机生成IP地址
export const randomIp = () => {
  return `192.168.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`
}

// 随机生成端口号
export const randomPort = () => {
  return Math.floor(Math.random() * 65535)
}

// 随机生成流量数据（字节）
export const randomTraffic = (min = 1024 * 1024, max = 1024 * 1024 * 1024) => {
  return Math.floor(min + Math.random() * (max - min))
}

// 随机生成速率数据（字节/秒）
export const randomSpeed = (min = 1024, max = 1024 * 1024) => {
  return Math.floor(min + Math.random() * (max - min))
}

// 上网管控 - 网站拦截规则
export const mockWebBlockingRules = ref([
  {
    id: '1',
    domain: 'facebook.com',
    type: 'domain',
    category: 'social',
    description: '社交媒体网站',
    enabled: true,
    hitCount: 15,
    lastHit: formatDate(randomDate())
  },
  {
    id: '2',
    domain: 'youtube.com',
    type: 'domain',
    category: 'video',
    description: '视频网站',
    enabled: true,
    hitCount: 8,
    lastHit: formatDate(randomDate())
  },
  {
    id: '3',
    domain: 'twitter.com',
    type: 'domain',
    category: 'social',
    description: '社交媒体网站',
    enabled: false,
    hitCount: 0,
    lastHit: null
  },
  {
    id: '4',
    domain: 'netflix.com',
    type: 'domain',
    category: 'video',
    description: '视频流媒体',
    enabled: true,
    hitCount: 3,
    lastHit: formatDate(randomDate())
  },
  {
    id: '5',
    domain: 'instagram.com',
    type: 'domain',
    category: 'social',
    description: '社交媒体网站',
    enabled: true,
    hitCount: 5,
    lastHit: formatDate(randomDate())
  }
])

// 上网管控 - IP拦截规则
export const mockIpBlockingRules = ref([
  {
    id: '1',
    ip: '203.0.113.1',
    description: '已知恶意IP',
    enabled: true,
    hitCount: 7,
    lastHit: formatDate(randomDate())
  },
  {
    id: '2',
    ip: '198.51.100.0/24',
    description: '广告服务器网段',
    enabled: true,
    hitCount: 12,
    lastHit: formatDate(randomDate())
  },
  {
    id: '3',
    ip: '192.0.2.1',
    description: '测试IP',
    enabled: false,
    hitCount: 0,
    lastHit: null
  },
  {
    id: '4',
    ip: '172.16.0.0/16',
    description: '内部网络',
    enabled: true,
    hitCount: 5,
    lastHit: formatDate(randomDate())
  }
])

// 上网管控 - 端口拦截规则
export const mockPortBlockingRules = ref([
  {
    id: '1',
    port: '3389',
    description: '远程桌面服务',
    enabled: true,
    hitCount: 5,
    lastHit: formatDate(randomDate())
  },
  {
    id: '2',
    port: '22',
    description: 'SSH服务',
    enabled: true,
    hitCount: 8,
    lastHit: formatDate(randomDate())
  },
  {
    id: '3',
    port: '1433',
    description: 'SQL Server',
    enabled: false,
    hitCount: 0,
    lastHit: null
  },
  {
    id: '4',
    port: '80,443',
    description: 'Web服务端口',
    enabled: true,
    hitCount: 15,
    lastHit: formatDate(randomDate())
  }
])

// 上网管控 - 拦截统计
export const mockBlockingStats = ref([
  {
    id: '1',
    timestamp: randomDate(),
    type: 'web',
    target: 'facebook.com',
    clientId: 'client-001',
    clientName: '办公室电脑-001',
    action: 'block',
    details: '社交媒体网站'
  },
  {
    id: '2',
    timestamp: randomDate(),
    type: 'ip',
    target: '203.0.113.1',
    clientId: 'client-002',
    clientName: '会议室电脑-002',
    action: 'block',
    details: '已知恶意IP'
  },
  {
    id: '3',
    timestamp: randomDate(),
    type: 'port',
    target: 'TCP:3389',
    clientId: 'client-001',
    clientName: '办公室电脑-001',
    action: 'block',
    details: '远程桌面服务'
  },
  {
    id: '4',
    timestamp: randomDate(),
    type: 'web',
    target: 'youtube.com',
    clientId: 'client-003',
    clientName: '开发机-003',
    action: 'block',
    details: '视频网站'
  },
  {
    id: '5',
    timestamp: randomDate(),
    type: 'web',
    target: 'netflix.com',
    clientId: 'client-002',
    clientName: '会议室电脑-002',
    action: 'block',
    details: '视频流媒体'
  }
])

// 应用流量管控 - 流量规则
export const mockTrafficRules = ref([
  {
    id: '1',
    appName: 'Chrome浏览器',
    path: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
    uploadLimit: 1024 * 1024 * 10, // 10MB/s
    downloadLimit: 1024 * 1024 * 20, // 20MB/s
    priority: 'high',
    enabled: true
  },
  {
    id: '2',
    appName: '微信',
    path: 'C:\\Program Files\\Tencent\\WeChat\\WeChat.exe',
    uploadLimit: 1024 * 1024 * 2, // 2MB/s
    downloadLimit: 1024 * 1024 * 5, // 5MB/s
    priority: 'medium',
    enabled: true
  },
  {
    id: '3',
    appName: '迅雷',
    path: 'C:\\Program Files\\Thunder Network\\Thunder\\Program\\Thunder.exe',
    uploadLimit: 1024 * 1024, // 1MB/s
    downloadLimit: 1024 * 1024 * 3, // 3MB/s
    priority: 'low',
    enabled: true
  }
])

// 应用流量管控 - 流量统计
export const mockTrafficStats = ref({
  upload: randomTraffic(1024 * 1024 * 100, 1024 * 1024 * 500), // 100-500MB
  download: randomTraffic(1024 * 1024 * 500, 1024 * 1024 * 2000), // 500MB-2GB
  currentSpeed: randomSpeed(1024 * 100, 1024 * 1024) // 100KB/s-1MB/s
})

// 应用流量管控 - 应用监控
export const mockAppTrafficMonitor = ref([
  {
    appName: 'Chrome浏览器',
    uploadSpeed: randomSpeed(1024 * 50, 1024 * 500),
    downloadSpeed: randomSpeed(1024 * 100, 1024 * 1000),
    totalUpload: randomTraffic(1024 * 1024 * 10, 1024 * 1024 * 100),
    totalDownload: randomTraffic(1024 * 1024 * 50, 1024 * 1024 * 500),
    connections: Math.floor(Math.random() * 20) + 1,
    icon: 'ChromeFilled',
    iconColor: '#4285F4'
  },
  {
    appName: '微信',
    uploadSpeed: randomSpeed(1024 * 10, 1024 * 100),
    downloadSpeed: randomSpeed(1024 * 20, 1024 * 200),
    totalUpload: randomTraffic(1024 * 1024 * 5, 1024 * 1024 * 50),
    totalDownload: randomTraffic(1024 * 1024 * 10, 1024 * 1024 * 100),
    connections: Math.floor(Math.random() * 5) + 1,
    icon: 'ChatDotRound',
    iconColor: '#07C160'
  },
  {
    appName: '迅雷',
    uploadSpeed: randomSpeed(1024 * 100, 1024 * 800),
    downloadSpeed: randomSpeed(1024 * 500, 1024 * 2000),
    totalUpload: randomTraffic(1024 * 1024 * 20, 1024 * 1024 * 200),
    totalDownload: randomTraffic(1024 * 1024 * 100, 1024 * 1024 * 1000),
    connections: Math.floor(Math.random() * 50) + 10,
    icon: 'Download',
    iconColor: '#3963BC'
  }
])

// 数据安全 - 加密目录
export const mockEncryptedFolders = ref([
  {
    id: '1',
    path: 'C:\\SecureDocuments',
    status: 'encrypted',
    algorithm: 'AES-256',
    size: '2.5GB',
    fileCount: 156,
    createdAt: formatDate(randomDate()),
    lastAccessed: formatDate(randomDate()),
    autoEncrypt: true
  },
  {
    id: '2',
    path: 'D:\\PrivateFiles',
    status: 'normal',
    algorithm: 'AES-256',
    size: '1.2GB',
    fileCount: 89,
    createdAt: formatDate(randomDate()),
    lastAccessed: formatDate(randomDate()),
    autoEncrypt: false
  },
  {
    id: '3',
    path: 'C:\\Users\\Admin\\Desktop\\Confidential',
    status: 'encrypting',
    algorithm: 'AES-256',
    size: '512MB',
    fileCount: 45,
    createdAt: formatDate(randomDate()),
    lastAccessed: formatDate(randomDate()),
    autoEncrypt: true
  }
])

// 数据安全 - USB设备
export const mockUsbDevices = ref([
  {
    id: '1',
    name: 'Kingston DataTraveler',
    type: 'storage',
    vendorId: '0x0951',
    productId: '0x1666',
    capacity: '32GB',
    status: 'connected',
    lastScan: formatDate(randomDate()),
    riskLevel: 'low'
  },
  {
    id: '2',
    name: 'SanDisk Cruzer',
    type: 'storage',
    vendorId: '0x0781',
    productId: '0x5567',
    capacity: '16GB',
    status: 'blocked',
    lastScan: formatDate(randomDate()),
    riskLevel: 'medium'
  },
  {
    id: '3',
    name: 'Logitech USB Mouse',
    type: 'input',
    vendorId: '0x046d',
    productId: '0xc077',
    capacity: '-',
    status: 'connected',
    lastScan: formatDate(randomDate()),
    riskLevel: 'low'
  },
  {
    id: '4',
    name: 'Unknown USB Device',
    type: 'unknown',
    vendorId: '0x1234',
    productId: '0x5678',
    capacity: '-',
    status: 'scanning',
    lastScan: formatDate(randomDate()),
    riskLevel: 'high'
  }
])

// 数据安全 - USB白名单设备
export const mockUsbWhitelist = ref([
  {
    id: '1',
    name: 'Kingston DataTraveler',
    vendorId: '0x0951',
    productId: '0x1666',
    description: '办公用U盘',
    addedAt: formatDate(randomDate())
  },
  {
    id: '2',
    name: 'Logitech USB Mouse',
    vendorId: '0x046d',
    productId: '0xc077',
    description: '办公鼠标',
    addedAt: formatDate(randomDate())
  }
])

// 应用分析数据
export const mockAppAnalysis = ref([
  {
    id: '1',
    name: 'Google Chrome',
    path: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
    type: 'browser',
    status: 'running',
    cpuUsage: Math.random() * 20,
    memoryUsage: Math.random() * 500 + 100, // MB
    networkActivity: randomSpeed(1024 * 10, 1024 * 500),
    riskLevel: 'low',
    lastActivity: formatDate(randomDate()),
    processCount: Math.floor(Math.random() * 10) + 1
  },
  {
    id: '2',
    name: '微信',
    path: 'C:\\Program Files\\Tencent\\WeChat\\WeChat.exe',
    type: 'communication',
    status: 'running',
    cpuUsage: Math.random() * 5,
    memoryUsage: Math.random() * 200 + 50,
    networkActivity: randomSpeed(1024 * 5, 1024 * 100),
    riskLevel: 'low',
    lastActivity: formatDate(randomDate()),
    processCount: Math.floor(Math.random() * 3) + 1
  },
  {
    id: '3',
    name: 'Suspicious Process',
    path: 'C:\\Temp\\unknown.exe',
    type: 'unknown',
    status: 'running',
    cpuUsage: Math.random() * 50 + 30,
    memoryUsage: Math.random() * 1000 + 200,
    networkActivity: randomSpeed(1024 * 100, 1024 * 1000),
    riskLevel: 'high',
    lastActivity: formatDate(randomDate()),
    processCount: 1
  },
  {
    id: '4',
    name: 'Microsoft Office',
    path: 'C:\\Program Files\\Microsoft Office\\Office16\\WINWORD.EXE',
    type: 'office',
    status: 'idle',
    cpuUsage: Math.random() * 2,
    memoryUsage: Math.random() * 100 + 30,
    networkActivity: 0,
    riskLevel: 'low',
    lastActivity: formatDate(randomDate()),
    processCount: 1
  }
])

// AI分析数据
export const mockAiAnalysis = ref({
  threatDetection: {
    totalThreats: 15,
    highRisk: 3,
    mediumRisk: 7,
    lowRisk: 5,
    lastScan: formatDate(randomDate())
  },
  behaviorAnalysis: {
    anomalousActivities: 8,
    suspiciousNetworkTraffic: 12,
    unusualProcessBehavior: 5,
    lastAnalysis: formatDate(randomDate())
  },
  recommendations: [
    {
      id: '1',
      type: 'security',
      priority: 'high',
      title: '检测到可疑进程',
      description: '发现进程 "unknown.exe" 存在异常网络活动',
      action: '建议立即终止该进程并进行深度扫描',
      timestamp: formatDate(randomDate())
    },
    {
      id: '2',
      type: 'performance',
      priority: 'medium',
      title: '内存使用率过高',
      description: 'Chrome浏览器内存使用超过500MB',
      action: '建议关闭不必要的标签页或重启浏览器',
      timestamp: formatDate(randomDate())
    },
    {
      id: '3',
      type: 'network',
      priority: 'low',
      title: '网络流量异常',
      description: '检测到大量上传流量',
      action: '建议检查是否有文件同步或备份任务',
      timestamp: formatDate(randomDate())
    }
  ]
})

// 获取虚拟数据的通用方法
export const getMockData = (type, options = {}) => {
  const { clientId, refresh = false } = options

  // 如果需要刷新数据，重新生成一些随机值
  if (refresh) {
    switch (type) {
      case 'trafficStats':
        mockTrafficStats.value = {
          upload: randomTraffic(1024 * 1024 * 100, 1024 * 1024 * 500),
          download: randomTraffic(1024 * 1024 * 500, 1024 * 1024 * 2000),
          currentSpeed: randomSpeed(1024 * 100, 1024 * 1024)
        }
        break
      case 'appTrafficMonitor':
        mockAppTrafficMonitor.value.forEach(app => {
          app.uploadSpeed = randomSpeed(1024 * 10, 1024 * 500)
          app.downloadSpeed = randomSpeed(1024 * 50, 1024 * 1000)
          app.connections = Math.floor(Math.random() * 20) + 1
        })
        break
      case 'appAnalysis':
        mockAppAnalysis.value.forEach(app => {
          app.cpuUsage = Math.random() * (app.name.includes('Suspicious') ? 80 : 20)
          app.memoryUsage = Math.random() * (app.name.includes('Suspicious') ? 1000 : 500) + 50
          app.networkActivity = randomSpeed(1024 * 5, 1024 * 500)
        })
        break
    }
  }

  // 根据类型返回对应的虚拟数据
  switch (type) {
    case 'webBlocking':
      return { rules: mockWebBlockingRules.value }
    case 'ipBlocking':
      return { rules: mockIpBlockingRules.value }
    case 'portBlocking':
      return { rules: mockPortBlockingRules.value }
    case 'blockingStats':
      return mockBlockingStats.value
    case 'appTraffic':
      return {
        rules: mockTrafficRules.value,
        stats: mockTrafficStats.value,
        monitor: mockAppTrafficMonitor.value
      }
    case 'encryptedFolders':
      return { folders: mockEncryptedFolders.value }
    case 'usbControl':
      return {
        devices: mockUsbDevices.value,
        whitelist: mockUsbWhitelist.value,
        policy: 'whitelist',
        readOnlyMode: false,
        autoScan: true
      }
    case 'appAnalysis':
      return mockAppAnalysis.value
    case 'aiAnalysis':
      return mockAiAnalysis.value
    default:
      return null
  }
}

// 模拟API延迟
export const mockApiDelay = (min = 300, max = 1000) => {
  const delay = Math.floor(Math.random() * (max - min + 1)) + min
  return new Promise(resolve => setTimeout(resolve, delay))
}









// 模拟API响应
export const mockApiResponse = async (data, success = true) => {
  await mockApiDelay()

  if (success) {
    return {
      success: true,
      data,
      message: '操作成功'
    }
  } else {
    throw new Error('模拟API错误')
  }
}
